package scheduler

import "gitee.com/wujianqiang/golibs/pkg/factory"

type SchCliConfig struct {
	NatConf     *factory.NatConf `yaml:"nat"`
	SubmitTopic string           `yaml:"subtopic"`
}
type SchClient struct {
	NatClient   *factory.NatClient
	SubmitTopic string
}

func NewSchClient(config SchCliConfig) (*SchClient, error) {
	natclient, er := config.NatConf.Connected()
	if er != nil {
		return nil, er
	}
	return &SchClient{
		NatClient:   natclient,
		SubmitTopic: config.SubmitTopic,
	}, nil
}
func (c *SchClient) SubmitJob(job *SubTask) error {
	data := map[string]interface{}{
		"action": "sub",
		"data":   job,
	}
	return c.NatClient.ReplyData(data, c.SubmitTopic)
}
func (c *SchClient) UpdateJob(job *SubTask) error {
	data := map[string]interface{}{
		"action": "update",
		"data":   job,
	}
	return c.NatClient.ReplyData(data, c.SubmitTopic)
}

// 耗时操作...
func (c *SchClient) DelJob(job *CancelTask) error {
	data := map[string]interface{}{
		"action": "del",
		"data":   job,
	}
	return c.NatClient.ReplyData(data, c.SubmitTopic)
}

func (c *SchClient) DelJobBak(job *CancelTask) {
	data := map[string]interface{}{
		"action": "del",
		"data":   job,
	}
	go func(data map[string]interface{}) {
		c.NatClient.ReplyData(data, c.SubmitTopic)
	}(data)
}
